<?php
/**
 * School  Controller
 *
 * School  Management
 *
 *
 * @version $Id: BaseController.php 77 2010-04-25 07:28:48Z yingkuang.chen@gmail.com $
 * @license New BSD License
 * @uses Zend_Controller_Action
 * @package school
 * @subpackage Controller
 */
class School_BaseController extends Zend_Controller_Action
{

    function indexAction()
    {
        $this->_redirect('school/base/set');
    }

    /**
     * download images
     * @return unknown_type
     */
    function getimgAction()
    {
//        $this->getHelper('layout')->disableLayout();
//        $this->getHelper('ViewRenderer')->setNorender();
        // 避免 jpg 等圖檔被 filter ,在檔尾加上"--"再還原
        $name = $this->_request->getParam('file');
        //echo $name; exit;
        $this->_helper->DownloadFile($name);
    }

    /**
     * save data
     * @return unknown_type
     */
    function setAction()
    {
        $request = $this->getRequest();

        $form = new School_Form_Base();

        if ($request->isPost()){
            $auth = Zend_Auth::getInstance();
            //	echo $form->view_picture->getValue();exit;
            $request->setParam('update_sn',$auth->getIdentity()->id);
            $request->setParam('update_ip',$request->getServer('REMOTE_ADDR'));
            if ($form->isValid($request->getParams())) {
                //儲存圖像
                $store_path = $this->_helper->getStorePath(); // 取得存放路徑
                $ss = 'view_picture_'.end(explode(".",$_FILES['view_picture_img']['name']));
                $file_name = $store_path.'/'.$ss;
                if(file_exists($_FILES['view_picture_img']['tmp_name'])){
                    if (is_file($store_path.'/'.$form->view_picture->getValue())) {
                        unlink($store_path.'/'.$form->view_picture->getValue());
                    }
                    move_uploaded_file($_FILES['view_picture_img']['tmp_name'],$file_name);
                    $request->setParam('view_picture',$ss);

                }

                $ss = 'position_picture_'.end(explode(".",$_FILES['position_picture_img']['name']));
                $file_name = $store_path.'/'.$ss;
                if(file_exists($_FILES['position_picture_img']['tmp_name'])){
                    if (is_file($store_path.'/'.$form->position_picture->getValue())) {
                        unlink($store_path.'/'.$form->position_picture->getValue());
                    }
                    move_uploaded_file($_FILES['position_picture_img']['tmp_name'],$file_name);
                    $request->setParam('position_picture',$ss);
                }


                $ss = 'logo_picture_'.end(explode(".",$_FILES['logo_picture_img']['name']));
                $file_name = $store_path.'/'.$ss;
                if(file_exists($_FILES['logo_picture_img']['tmp_name'])){
                    if (is_file($store_path.'/'.$form->logo_picture->getValue())) {
                        unlink($store_path.'/'.$form->logo_picture->getValue());
                    }
                    move_uploaded_file($_FILES['logo_picture_img']['tmp_name'],$file_name);
                    $request->setParam('logo_picture',$ss);
                }

                $ss = 'banner_picture_'.end(explode(".",$_FILES['banner_picture_img']['name']));
                $file_name = $store_path.'/'.$ss;
                if(file_exists($_FILES['banner_picture_img']['tmp_name'])){
                    if (is_file($store_path.'/'.$form->banner_picture->getValue())) {
                        unlink($store_path.'/'.$form->banner_picture->getValue());
                    }
                    move_uploaded_file($_FILES['banner_picture_img']['tmp_name'],$file_name);
                    $request->setParam('banner_picture',$ss);
                }

                $School = new School_Model_School();
                $School->assignIdentifier($request->getParam('id'));
                $School->fromArray($request->getParams());
                $School->id = $request->getParam('show_id');
                $School->save();
                $cache = Zend_Registry::get('cache');
                $cache->remove('school');
                $this->_redirect('school/base/set');

            }

        }

        $school = new School_Model_School();

        $year_name = $school->getYearName();
        if ($this->view->year_name)
        $year_name[$this->view->School['length']] = $this->view->year_name;
        $this->view->year_name = Zend_Json::encode($year_name);

        $this->view->form = $form;
    }

    /**
     * 設定年級名稱
     * @return JSON
     */
    function setyearAction()
    {

        $length_name = serialize($this->getRequest()->getParam('year_name'));
        $id =  strip_tags($this->getRequest()->getParam('id'));
        $School = new School_Model_School();
        $School->assignIdentifier($id);
        $School->length_name = $length_name;
        $School->save();

        // 重設acl
        $cache = Zend_Registry::get('cache');
        $cache->remove('school');
        $this->_helper->json($this->_request->getParam('year_name'));

    }
}